// Copyright (c) 2022 Presto Labs Pte. Ltd.
// Author: gekim

#pragma once

#include <map>
#include <string>
#include <vector>

#include "coin2/exchange/base/executor_util/market_ts.h"
#include "coin2/exchange/base/feed/processor.h"
#include "coin2/exchange/prex_futures/api_util/auth_key.h"

namespace coin2::exchange::prex_base::feed_v1 {
namespace impl {

class PrexBaseRequestGenerator : public base::feed::RequestGenerator {
 public:
  std::vector<std::string> GenerateRequests(
    const std::vector<std::string>& channels,
    const std::vector<std::string>& symbols,
    const std::map<std::string, std::string>& channel_templates,
    const std::string& request_template) override {
    std::vector<std::string> reqs;

    std::vector<std::string> ch = channels;
    auto it = std::find(ch.begin(), ch.end(), "mark_price");
    if (it != ch.end()) {
      std::string c = channel_templates.at("mark_price");
      std::string s = request_template;
      boost::replace_all(s, "{channel}", c);
      reqs.push_back(s);
      ch.erase(it);
    }

    std::vector<std::string> reqs_others = RequestGenerator::GenerateRequests(
        ch, symbols, channel_templates, request_template);
    reqs.insert(reqs.end(), reqs_others.begin(), reqs_others.end());

    return reqs;
  }

  virtual std::vector<std::pair<std::string, std::string>> GenerateHeader(
      const std::string& key_filepath) override {
    std::vector<std::pair<std::string, std::string>> headers;

    std::string prex_api_key;
    std::string prex_api_expires;
    std::string prex_api_sign;
    std::string prex_auth_msg;

    auto prex_auth = PrexFuturesWsApiKeyAuth(key_filepath);
    prex_auth.CreateSignature(&prex_api_key, &prex_api_expires, &prex_api_sign, &prex_auth_msg);

    headers.emplace_back(std::make_pair("api-key", prex_api_key));
    headers.emplace_back(std::make_pair("api-expires", prex_api_expires));
    headers.emplace_back(std::make_pair("api-signature", prex_api_sign));

    return headers;
  }
};

}  // namespace impl
using impl::PrexBaseRequestGenerator;
}  // namespace coin2::exchange::prex_base::feed_v1
